<template>
	<view>
		<uv-notify ref="notify" customStyle="z-index:9999"></uv-notify>
		<div class="add-bike">
		  <h1 class="title">添加自行车</h1>
		  <form @submit.prevent="addBike" class="form">
		    <label for="batteryId" class="label">电池ID:</label>
		  
		    <uv-input placeholder="请输入数字" border="surround" v-model="batteryId" class="input" clearable ></uv-input>
		    <label for="latitude" class="label">纬度:</label>
		  
		    <uv-input placeholder="请输入数字" border="surround" v-model="latitude" class="input" clearable ></uv-input>
		    <label for="longitude" class="label">经度:</label>
		    
		    <uv-input placeholder="请输入数字" border="surround" v-model="longitude" class="input" clearable ></uv-input>
		    <label for="lightThreshold" class="label">光照阈值:</label>
		   
		    <uv-input placeholder="请输入数字" border="surround" v-model="lightThreshold" class="input" clearable ></uv-input>
		    <button type="submit" class="button" @click="addBike">添加自行车</button>
		  </form>
		</div>
	</view>
	
</template>

<script>
import { func } from '../../../uni_modules/uv-ui-tools/libs/function/test';
export default {
  data() {
    return {
	  apiUrl: '',
	  setToken: func(),
	  header: {},
      batteryId: '',
      latitude: 0,
      longitude: 0,
      lightThreshold: 0
    };
  },
  mounted() {
  	this.apiUrl = getApp().globalData.apiUrl;
  	// setToken是设置header放入token的函数
  	this.setToken = getApp().globalData.setToken;
  	this.header = this.setToken();
  	/* 如果没有用户信息或者无token直接返回登录重新获取 */
  	if (this.user == '' || this.setToken == null) {
		this.promptFail("请先登录！");
  		setTimeout(function() {
  			uni.navigateTo({
  				url: '/pages/user/login'
  			});
  		}, 1000);
  	}
  },
  methods: {
	  /* 成功提示 */
	  promptSuccess(msg) {
	  	this.$refs.notify.success(msg);
	  },
	  promptFail(msg) {
	  	this.$refs.notify.error(msg);
	  },
     addBike() {
          // 构造要发送的数据
          const bikeData = {
            batteryId: this.batteryId,
            latitude: this.latitude,
            longitude: this.longitude,
            lightThreshold: this.lightThreshold
          };
          
          /* 查询所有可租借的车（无论是否借车） */
          const backendURL = this.apiUrl + "/bicycle/addOne";
          let header = this.setToken();
          let satoken = header.satoken;
          uni.request({
          	url: backendURL,
          	method: 'POST',
          	dataType: 'json',
          	data: JSON.stringify(bikeData), // 将user对象转换为JSON字符串
          	header: {
          		'content-type': 'application/json', // 设置content-type为application/json
          		'satoken': satoken
          	},
          	success: (res) => {
          		console.log('成功', res);
          		console.log(res.data);
				if(res.data.msg==200)
				{
					this.promptSuccess(res.data.msg);
				}
          		this.promptFail("请检查输入内容是否合法");
          	},
          	fail: function(res) {
          		console.log('错误', res);
				thi.promptFail("添加失败");
          	}
          });
        }
  }
};
</script>

<style scoped>
.add-bike {
  max-width: 400px;
  margin: 0 auto;
  padding: 20px;
  text-align: center;
}

.title {
  font-size: 24px;
  margin-bottom: 20px;
}

.form {
  display: flex;
  flex-direction: column;
}

.label {
  font-size: 16px;
  margin-bottom: 5px;
}

.input {
  padding: 8px;
  border: 1px solid #ccc;
  border-radius: 4px;
  margin-bottom: 10px;
}

.button {
  background-color: #007bff;
  color: #fff;
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
}

.button:hover {
  background-color: #0056b3;
}
</style>
